package com.lwy.unos.stuinfo.service.impl;

import com.lwy.common.utils.R;
import com.lwy.unos.stuinfo.dao.ClassInfoDao;
import com.lwy.unos.stuinfo.dao.GradeInfoDao;
import com.lwy.unos.stuinfo.entity.ClassInfoEntity;
import com.lwy.unos.stuinfo.entity.GradeInfoEntity;
import com.lwy.unos.stuinfo.vo.StuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.unos.stuinfo.dao.StuInfoDao;
import com.lwy.unos.stuinfo.entity.StuInfoEntity;
import com.lwy.unos.stuinfo.service.StuInfoService;

@Service("stuInfoService")
public class StuInfoServiceImpl extends ServiceImpl<StuInfoDao, StuInfoEntity> implements StuInfoService {

    @Autowired
    GradeInfoDao gradeInfoDao;

    @Autowired
    ClassInfoDao classInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StuInfoEntity> page = this.page(
                new Query<StuInfoEntity>().getPage(params),
                new QueryWrapper<StuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public StuInfoVo getByStuId(Long stuId) {
        StuInfoVo stuInfoVo = getStuInfoVo(stuId);
        return stuInfoVo;
    }

    private StuInfoVo getStuInfoVo(Long stuId) {
        StuInfoVo stuInfoVo = new StuInfoVo();
        StuInfoEntity entity = baseMapper.selectById(stuId);
        stuInfoVo.setStuId(stuId);
        stuInfoVo.setStuName(entity.getStuName());
        stuInfoVo.setStuAge(entity.getStuAge());
        stuInfoVo.setStuPhone(entity.getStuPhone());
        stuInfoVo.setStuSex(entity.getStuSex() == 0 ? "男" : "女");

        GradeInfoEntity gradeInfoEntity = gradeInfoDao.selectById(entity.getStuGradeId());

        ClassInfoEntity classInfoEntity = classInfoDao.selectById(entity.getStuClassId());

        stuInfoVo.setStuGrade(gradeInfoEntity.getGradeName());

        stuInfoVo.setStuClass(classInfoEntity.getClassName());
        return stuInfoVo;
    }

    @Override
    public List<StuInfoVo> lists() {
        List<StuInfoEntity> list = list();
        List<StuInfoVo> stuInfoVos = list.stream().map(entity -> {
            StuInfoVo stuInfoVo = getStuInfoVo(entity.getStuId());
            return stuInfoVo;
        }).collect(Collectors.toList());
        return stuInfoVos;
    }


}